import './index.less';

declare const layer: any;
declare const $: any;

$(document).on("click", ".showTk", function () {
    $(".mask").addClass("show");
    $(".error-tips").html("");
});
$(document).on("click", ".close", function () {
    $(".mask").removeClass("show");
    $(this).parents(".mask").find("form")[0].reset();
    $("select").css("color", "#999");
});
var inputs = [];
$.each($("form").find("[name]"), (acc: number, item: any) => {
    if ($(item).attr("_placeholder") || $(item).attr("placeholder")) {
        inputs.push({
            ele: item,
            message: $(item).attr("_placeholder") || $(item).attr("placeholder"),
        })
    }
})
$("button[type=submit]").on("click", function () {
    var form: any = $(this).closest("form");
    var errors = [];
    var msg = inputs.reduce(function (acc, item) {
        if (form.find($(item.ele)) && form.find($(item.ele)).length && !$(item.ele).val()) {
            errors.push(item.message)
        }
        return errors[0];
    }, "");
    if (msg) {
        layer.msg(msg);
        if (form.find(".error-tips") && form.find(".error-tips").length) {
            $(".error-tips").html(msg);
        }
        return false;
    }
    else {
        $.ajax({
            url: "".concat(form[0].action),
            type: form[0].method,
            dataType: "JSON",
            data: form.serialize(),
            success: function (res) {
                if (res.status == 1) {
                    $('.mask_form').removeClass('show');
                    layer.msg("您已报名成功，谢谢您的参与！");
                    $(document).find(".close").click();
                    form[0].reset();
                    $("select").css("color", "#999");
                }
                else {
                    layer.msg(res.info);
                }
            },
            error: function () {
                layer.msg("报名失败，请稍后再试");
            }
        });
    }
    return false;
});
$("#go-top").on("click", function () {
    $('html,body').animate({
        scrollTop: 0
    }, 500);
});
$("select").change(function () {
    $(this).css("color", "#000");
});
$('#share').share({
    sites: ['wechat']
});